// --------------------------------------------------------------------------------------------------------------------
// <copyright file="UniverseExpo.cs" company="Open Trader">
//   Copyright (c) David Denis (david.denis@systemathics.com)
// </copyright>
// <summary>
//   |  Open Trader - The Open Source Systematic Trading Platform
//   |
//   |  This program is free software: you can redistribute it and/or modify
//   |  it under the terms of the GNU General Public License as published by
//   |  the Free Software Foundation, either version 2 of the License, or
//   |  (at your option) any later version.
//   |
//   |  This program is distributed in the hope that it will be useful,
//   |  but WITHOUT ANY WARRANTY; without even the implied warranty of
//   |  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//   |  GNU General Public License for more details.
//   |
//   |  You should have received a copy of the GNU General Public License
//   |  along with this program.  If not, see http://www.gnu.org/licenses
//   |
//   |  Up to date informations about Open Trader can be found at :
//   |    http://opentrader.org
//   |    http://opentrader.codeplex.com
//   |
//   |  For professional services, please visit us at :
//   |    http://www.systemathics.com
// </summary>
// --------------------------------------------------------------------------------------------------------------------

namespace Org.OpenTrader.Framework.Forge
{
    #region Using Directives

    using Org.OpenTrader.Framework.Enums;
    using Org.OpenTrader.Framework.Forge.Enums;
    using Org.OpenTrader.Framework.Forge.Interfaces;

    #endregion

    /// <summary>
    /// The strategy expo
    /// </summary>
    public class UniverseExpo : Expo<IUniverseExpoCell>
    {
        private static readonly Identity Identity = Identity.Create(System.Reflection.MethodBase.GetCurrentMethod());

        #region Constructors and Destructors

        /// <summary>
        /// Initializes a new instance of the <see cref="UniverseExpo"/> class. 
        /// Create Universe expo
        /// </summary>
        /// <param name="scope">
        /// The scope.
        /// </param>
        public UniverseExpo(ETradingScope scope)
        {
            Logging.Log(Identity, ELogLevel.Debug, () => string.Format("Creating new UniverseExpo ({0})", scope));

            // TradingScope
            this.TradingScope = scope;

            // Traded
            this[EExpoCellType.Traded] = new UniverseExpoCell(EExpoCellType.Traded);

            // Working
            this[EExpoCellType.Working] = new UniverseExpoCell(EExpoCellType.Working);

            // Inventory
            this[EExpoCellType.Inventory] = new UniverseExpoCell(EExpoCellType.Inventory);

            // Target
            this[EExpoCellType.Target] = new UniverseExpoCell(EExpoCellType.Target);

            // Pending
            this[EExpoCellType.Pending] = new UniverseExpoCell(EExpoCellType.Pending);
        }

        #endregion

        #region Properties

        /// <summary>
        /// Gets TradingScope
        /// </summary>
        public ETradingScope TradingScope { get; set; }

        #endregion

        #region Public Methods

        /// <summary>
        /// Add IInstrumentView
        /// </summary>
        /// <param name="i">
        /// </param>
        /// <returns>
        /// The add.
        /// </returns>
        public virtual bool Add(IInstrumentView i)
        {
            if (i == null)
            {
                Logging.Log(Identity, ELogLevel.Error, () => string.Format("Add : Cannot add InstrumentView, Invalid InstrumentView"));
                return false;
            }

            Logging.Log(Identity, ELogLevel.Debug, () => string.Format("Add : adding IInstrumentView {0}", i.Code));
            foreach (var cell in this.Cells)
            {
                if (cell != null)
                {
                    var expo = i[this.TradingScope][cell.ExpoCellType];
                    if (expo != null)
                    {
                        cell.Add((InstrumentListExpoCell)expo);
                    }
                    else
                    {
                        Logging.Log(
                            Identity, 
                            ELogLevel.Error, 
                            () => string.Format("Add : cannot add InstrumentListExpoCell {0} : {1} - {2}", i.Code, this.TradingScope, cell.ExpoCellType));
                    }
                }
            }

            return true;
        }

        /// <summary>
        /// Remove IInstrumentView
        /// </summary>
        /// <param name="i">
        /// </param>
        /// <returns>
        /// The remove.
        /// </returns>
        public virtual bool Remove(IInstrumentView i)
        {
            if (i == null)
            {
                Logging.Log(Identity, ELogLevel.Error, () => string.Format("Remove : Cannot remove InstrumentView, Invalid InstrumentView"));
                return false;
            }

            Logging.Log(Identity, ELogLevel.Debug, () => string.Format("Remove : removing IInstrumentView {0}", i.Code));
            foreach (var cell in this.Cells)
            {
                if (cell != null)
                {
                    var expo = i[this.TradingScope][cell.ExpoCellType];
                    if (expo != null)
                    {
                        cell.Remove((InstrumentListExpoCell)expo);
                    }
                    else
                    {
                        Logging.Log(
                            Identity, 
                            ELogLevel.Error, 
                            () => string.Format("Remove : cannot Remove InstrumentListExpoCell {0} : {1} - {2}", i.Code, this.TradingScope, cell.ExpoCellType));
                    }
                }
            }

            return true;
        }

        #endregion
    }
}